package com.sdry.approval.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.approval.constant.ApprovalTypeEnum;
import com.sdry.approval.entity.BaseRequest;
import com.sdry.approval.entity.MeSponsor;
import com.sdry.approval.mapper.BaseRequestMapper;
import com.sdry.approval.mapper.LeaveRequestMapper;
import com.sdry.approval.mapper.MeSponsorMapper;
import com.sdry.untils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: info_sdry
 * @description: 通用申请
 * @author: cb
 * @create: 2020-02-21 18:16
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseRequestServiceImpl extends ServiceImpl<BaseRequestMapper, BaseRequest> implements BaseRequestService{

    @Autowired
    private BaseRequestMapper baseRequestMapper;

    @Autowired
    private LeaveRequestMapper leaveRequestMapper;

    @Autowired
    private MeSponsorService meSponsorService;

    @Override
    public boolean add(BaseRequest baseRequest, String[] userId) {
        baseRequest.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());
        //通用表添加数据
        Integer effect = baseRequestMapper.insert(baseRequest);
        if (userId != null){
            for (String str : userId) {
                leaveRequestMapper.addLeaveMan(str,baseRequest.getBaseId(),"sp","ty");
            }

        }
        //人员通用表添加
        if (baseRequest.getCopyGiveBy() != null){
            leaveRequestMapper.addLeaveMan(baseRequest.getCopyGiveBy().toString(),baseRequest.getBaseId(),"cs","ty");
        }

        //返回影响行数
        if (effect == 0){
            return false;
        }else {
            //我发起的
            meSponsorService.addMeSponsor(ApprovalTypeEnum.TYPE_APPROVAL_TY.getCode(),baseRequest.getBaseId());
            return true;
        }
    }
}
